package com.alex.util.security.aes;


import com.alex.util.security.test.ByteGroup;
import com.alex.util.security.test.ConstantInterface;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author alex
 */
public class AesUtils {
    public AesUtils() {
    }

    public static Map<String, String> decrypt(String text, byte[] aesKey) throws Exception {
        Map<String, String> result = new HashMap();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
        IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
        cipher.init(2, key_spec, iv);
        byte[] encrypted = text.getBytes();
        byte[] original = cipher.doFinal(encrypted);
        byte[] networkOrder = Arrays.copyOfRange(original, 16, 20);
        int jsonLength = recoverNetworkBytesOrder(networkOrder);
        String jsonContent = new String(Arrays.copyOfRange(original, 20, 20 + jsonLength), ConstantInterface.CHARSET);
        String from_oavsId = new String(Arrays.copyOfRange(original, 20 + jsonLength, original.length), ConstantInterface.CHARSET);
        result.put("content", jsonContent);
        result.put("from_oavsId", from_oavsId);
        return result;
    }

    static byte[] getNetworkBytesOrder(int sourceNumber) {
        byte[] orderBytes = new byte[]{(byte) (sourceNumber >> 24 & 255), (byte) (sourceNumber >> 16 & 255), (byte) (sourceNumber >> 8 & 255), (byte) (sourceNumber & 255)};
        return orderBytes;
    }

    static int recoverNetworkBytesOrder(byte[] orderBytes) {
        int sourceNumber = 0;

        for (int i = 0; i < 4; ++i) {
            sourceNumber <<= 8;
            sourceNumber |= orderBytes[i] & 255;
        }

        return sourceNumber;
    }

    public static String encrypt(String randomStr, String text, String appId, byte[] aesKey) throws NoSuchAlgorithmException {
        ByteGroup byteCollector = new ByteGroup();
        byte[] randomStrBytes = randomStr.getBytes(ConstantInterface.CHARSET);
        byte[] textBytes = text.getBytes(ConstantInterface.CHARSET);
        byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length);
        byte[] appIdBytes = appId.getBytes(ConstantInterface.CHARSET);
        byteCollector.addBytes(randomStrBytes);
        byteCollector.addBytes(networkBytesOrder);
        byteCollector.addBytes(textBytes);
        byteCollector.addBytes(appIdBytes);
        byte[] unencrypted = byteCollector.toBytes();

        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec skeySpec = new SecretKeySpec(aesKey, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(aesKey, 0, 16);
            cipher.init(1, skeySpec, ivParameterSpec);
            byte[] encrypted = cipher.doFinal(unencrypted);
            String base64Encrypted = new String(encrypted);
            return base64Encrypted;
        } catch (Exception var15) {
            var15.printStackTrace();
            return null;
        }
    }

    public static String getRandomStr(int n) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < n; ++i) {
            int number = random.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".length());
            sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".charAt(number));
        }

        return sb.toString();
    }
}
